
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Promise练习</title>
</head>
<body>
    
</body>
<script>
    //创建并定义一个新的Promise对象
    const p = new Promise((resolve,reject)=>{
        //执行异步操作
            setTimeout(()=>{
                //如果当前事件是偶数就代表成功，否则就代表失败
                const time = Date.now();
                console.log(time)
                if(time %2 === 0){
                    //成功的调用
                    resolve("成功了");
                }else{
                    //失败了调用
                    reject("失败了");
                }
            },1000);
    });
    

    setTimeout(()=>{
        p.then(
            value =>{  //接收得到成功的value的数据
                console.log(value);
            },
            reason =>{//接收得到失败的reason的数据
                console.log(reason);
            }
        )
    },2000)
    

  
    setHui=()=>{
        console.log("这是set")
    }
  
    
    setHui(2);

    setHui =(a,getHui,setHui)=>{
        
        setTimeout(()=>{
            if(a === 0){
                getHui();
            }else(
                setHui()    
            )
        },1000)
    };

    async function show(){
        return "sss"
    };
    const s = show();
    console.log(s);



    const newSet = new Promise((resolve,reject)=>{
        setTimeout(()=>{
            console.log("成功");
            resolve();
        },1000)
    }).then(
        value =>{
            throw 5;
        }
    ).catch((reason) =>{
        console.log("catch"+reason)
    })

    console.log(newSet," zheg ");


    //Promise.all([p,p1,p2])

    // const p1 = new Promise((resolve,reject)=>{
    //     resolve();
    // })
    // const p2 = new Promise((resolve,reject)=>{
    //     reject();
    // })
    // Promise.reject()

    f1 = () =>{
        return new Promise((resolve,reject)=>{
            // resolve(1);
            reject("错误")
        })
    }

    async function test(){
        try{
           const p =  await f1();
           console.log(p)
        }catch(error){
            console.error(error)
        }
    }
    test();

    async function test2(){
        return 1;
    }
    console.log(test2());

    async function test3(){
        return new Promise((resolve,reject)=>{
            resolve("test3")
        });
    }



   test3().then(
       value =>{
            console.log("这个是成功的value");
       },
       reason =>{
           console.log("reason")
        }
   )
    

   history.back();
   history.forward();
   history.listen(()=>{
       console.log("监听路径变化")
   });
   history.replaceState


</script>
</html>